{extend name="base" /}
{block name="body"}
<div class="layui-body">
    <!--tab标签-->
    <div class="layui-tab layui-tab-brief">
        <ul class="layui-tab-title">
            <li class="layui-this">游戏管理</li>
            <li class=""><a href="{:url('/admin/game/add')}">添加游戏</a></li>
        </ul>
        <div class="layui-tab-content">
            <div class="layui-tab-item layui-show">

                <form class="layui-form layui-form-pane" action="{:url('/admin/game')}" method="get">
                    <div class="layui-inline">
                        <label class="layui-form-label">游戏名称</label>
                        <div class="layui-input-inline">
                            <input type="text" name="name" value="{:input('name')}" placeholder="游戏名称" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline mleft">
                        <label class="layui-form-label">选择游戏</label>
                        <div class="layui-input-inline" style="width: 120px;">
                            <select name="game" lay-filter="game" id="game" lay-search>
                                <option value="">请选择游戏</option>
                                {foreach name=":get_game_list()" item="vo"}
                                    <option value="{$vo.id}"{if condition="input('game') == $vo.id"}selected{/if}>{$vo.name}</option>
                                {/foreach}
                            </select>
                        </div>
                    </div>

                    <div class="layui-inline mleft">
                        <label class="layui-form-label">CP名称</label>
                        <div class="layui-input-inline" style="width: 120px;">
                            <select name="gchannel" lay-filter="game" id="game" lay-search>
                                <option value="">请选择游戏</option>
                                {foreach name=":get_cp_list()" item="vo"}
                                    <option value="{$vo.id}"{if condition="input('gchannel') == $vo.id"}selected{/if}>{$vo.name}</option>
                                {/foreach}
                            </select>
                        </div>
                    </div>
                    
                    <div class="layui-inline">
                        <button class="layui-btn">搜索</button>
                    </div>
                </form>
                <hr>

                <table class="layui-table">
                    <thead>
                    <tr>
                        <th style="width: 45px;">游戏ID</th>
                        <th style="width: 50px;">游戏名称</th>
                        <th style="width: 30px;">CP</th>
                        <th style="width: 60px;">游戏状态</th>
                        <th style="width: 60px;">上线/禁用</th>
                        <th style="width: 45px;">对接参数</th>
{/*						<th >游戏登陆地址</th>*/}
{/*						<th >支付通知地址</th>*/}
                        <th style="width: 30px;">操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    {foreach name="game_list" item="vo"}
                    <tr>
                        <td>{$vo.id}</td>
                        <td>{$vo.name}</td>
                        <td>{:get_cp_name($vo['gchannel'])}</td>
                        <td>
                            {if condition="$vo.gstatus == 0"}
                            禁用
                            {elseif condition="$vo.gstatus eq 1"}
                            已上线
                            {elseif condition="$vo.gstatus eq 2"}
                            测试中
                            {/if}
                        </td>
                        <td class="layui-form">
						    <div class="layui-form layui-form-item">
                                {if condition="$vo.gstatus == 0"}
                                    <a style="color: #1068bf;cursor: pointer;" class="ajax-post" data-id="{$vo.id}" data-status="1" data-url="{:url('/admin/game/check')}">启用</a>
                                {elseif condition="$vo.gstatus eq 1"}
                                    <a style="color: #1068bf;cursor: pointer;" class="ajax-post" data-id="{$vo.id}" data-status="0" data-url="{:url('/admin/game/check')}">禁用</a>
                                {elseif condition="$vo.gstatus eq 2"}
                                    <a style="color: #1068bf;cursor: pointer;" class="ajax-post" data-id="{$vo.id}" data-status="1" data-url="{:url('/admin/game/check')}">上线</a>
                                {/if}
							</div>
						</td>
                        <td><span onclick="gameConfig(this)" data-id="{$vo.id}" data-name="{$vo.name}"
                                data-login_key="{$vo.login_key}" data-pay_key="{$vo.pay_key}" class="layui-btn layui-btn-normal layui-btn-mini">查看</span></td>
{/*						<td>{$vo.api_login_url&cc;</td>*/}
{/*						<td>{$vo.api_pay_url&cc;</td>*/}
                        <td>
                            <a href="{:url('/admin/game/edit',['id'=>$vo['id']])}" class="layui-btn layui-btn-mini">编辑</a>
                            {/*<a href="{:url('/admin/game/delete',['id'=>$vo['id']])&cc;" class="layui-btn layui-btn-danger layui-btn-mini ajax-delete">删除</a>*/}
                        </td>
                    </tr>
                    {/foreach}
                    </tbody>
                </table>
                <!--分页-->
                {$game_list|raw}
            </div>    
        </div>
    </div>
</div>
{/block}
{block name="script"}
<script>
$('.ajax-post').click(function (){
    var url = $(this).data('url');
    var id = $(this).data('id');
    var gstatus = $(this).data('status');
    $.post(url,{id:id,val:gstatus,tp:'gstatus'},function(info){
        layer.msg(info.msg);
        setTimeout(() => {
            location.reload();
        }, 1000); // 1秒后刷新
    })
});
//查看对接参数
function gameConfig(element) {
    var game_id = $(element).attr('data-id');
    var game_name = $(element).attr('data-name');
    var login_key = $(element).attr('data-login_key');
    var pay_key = $(element).attr('data-pay_key');
    var game_url = '{$game_url}/'+game_id;
    var text = '游戏ID：'+game_id+'\n游戏名称：'+game_name+'\n登录key：'+login_key+'\n支付key：'+pay_key+'\n游戏地址：'+game_url;
    layer.open({
        type: 1,
        title: '游戏对接参数',
        area: ['500px', '390px'],
        shadeClose: true, //点击遮罩关闭
        btn: ['复制参数', '关闭'], // 自定义按钮
        btnAlign: 'c', // 按钮居中
        yes: function(index){
            copyToClipboard(text);
            layer.msg('复制成功');
        },
        btn2: function(index){
            // 关闭按钮回调
            layer.close(index);
        },
        content: '<div style="padding: 20px;">' +

                '<div class="layui-form-item">' +
                '<label class="layui-form-label">游戏ID</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" name="game_id" value="'+game_id+'" placeholder="游戏ID" class="layui-input" readonly>' +
                '</div>' +
                '</div>' +

                '<div class="layui-form-item">' +
                '<label class="layui-form-label">游戏名称</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" name="game_name" value="'+game_name+'" placeholder="游戏名称" class="layui-input" readonly>' +
                '</div>' +
                '</div>' +

                '<div class="layui-form-item">' +
                '<label class="layui-form-label">登录key</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" name="login_key" value="'+login_key+'" placeholder="登录key" class="layui-input" readonly>' +
                '</div>' +
                '</div>' +

                '<div class="layui-form-item">' +
                '<label class="layui-form-label">支付key</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" name="pay_key" value="'+pay_key+'" placeholder="支付key" class="layui-input" readonly>' +
                '</div>' +
                '</div>' +

                '<div class="layui-form-item">' +
                '<label class="layui-form-label">游戏地址</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" name="game_url" value="'+game_url+'" placeholder="游戏地址" class="layui-input" readonly>' +
                '</div>' +
                '</div>' +

                '</div>',

    });
}

function copyToClipboard(text) {
    const textarea = document.createElement('textarea');
    textarea.value = text;
    document.body.appendChild(textarea);
    textarea.select();
    document.execCommand('copy');
    document.body.removeChild(textarea);
}
</script>
{/block}